<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>修改仓库</p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body">
        <div class="line">
          <span class="lab"><i class="mark_icon">*</i>关联仓库：</span>
          <div :class="{'error_border':tip_num===1}">
            <el-select
              filterable
              v-model="warehouseId"
              placeholder="请选择关联仓库">
              <el-option
                v-for="(item,index) in warehouseOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
        <p class="error_line">{{ infoTip }}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getDetail,changeWarehouse } from "@/api/data/user/address";
import {getAllWarehouse} from "@/api/basic/warehouse";

export default {
  props:['addressId'],
  data(){
    return {
      warehouseId:'',
      warehouseOption:[],
      infoTip: '',
      tip_num: '',
      loading:false
    }
  },
  mounted() {
    this.getAllHouse();
    if (this.addressId){
      this.getDetail();
    }
  },
  methods:{
    getDetail(){
      let that = this;
      this.loading = true;
      getDetail(that.addressId).then(res => {
        if (res){
          this.warehouseId = res.warehouseId?res.warehouseId:'';
        }
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    getAllHouse(){
      let that = this;
      getAllWarehouse('').then(res=>{
        that.warehouseOption = res ? res: [];
      }).catch((e)=>{
        that.warehouseOption = [];
      })
    },

    // 确认编辑
    confirmEdit(){
      if (this.warehouseId === '') {
        this.setTip(1, '请选择需要更换的仓库！');
      }else {
        this.sendInfo();
      }
    },

    sendInfo(){
      let that = this;
      that.loading = true;
      let data = {
        id: that.addressId,
        warehouseId:that.warehouseId
      }
      changeWarehouse(data).then(response => {
        that.closePopup(true);
        that.$message.success({
          message:'操作成功！',
          duration:1500
        });
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    setTip(num,text){
      this.tip_num = num;
      this.infoTip = text
    },

    // 关闭
    closePopup(refresh){
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>
.half_box div {
  width: 124px !important;
}
.half_box .second_lab {
   width: 75px;
}
</style>
